var index_books_order = 1;

$(document).ready(function (){
	view_init();
	event_init();
});

function view_init()
{
	loadingStart();
	
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
	
	// 初始化借书单分类
	um_ajax_post(
			"/loanorder/catalog/select.do",
			null,
			function (data){
				if (data && data.catalogs)
				{
					for (var i=0;i<data.catalogs.length;i++)
					{
						$("[data-id=status]")
							.append('<option value="'+data.catalogs[i].catalogID+'">'
										+data.catalogs[i].catalogName+'</option>');
					}
				}
			},
			null);
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
	// 查询按钮
	$("#query").click(function (){
		var createTimeStringStart = $("[data-id=createTimeStringStart]").val();
		var createTimeStringEnd = $("[data-id=createTimeStringEnd]").val();
		if (createTimeStringStart && createTimeStringEnd)
		{
			if (moment(createTimeStringStart).isAfter(createTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}
		$("#list_tbody").html("");
		list_list(true);
	});
	
	
	// 详情按钮
	$("#list_tbody").delegate("[data-id=detail]" ,"click" ,function (){
		var tempId = $(this).parent().attr("data-id");
		if (tempId==null||tempId == undefined||tempId == "")
		{
			$("body").umDialog("alert" ,{"content":"订单编号异常！",left:"40%"});
		}else{
			window.location.href = "/loanorder/direction/detail.do?id="+$(this).parent().attr("data-id");
		}
	});
	
	
	// 翻页事件
	index_initPageEvent("list_list()");
	
	// 导出excel
	$("#export").click(function (){
		var orderNoArr = getCheckdOrderNo();
		if(orderNoArr.length > 0)
		{
			var paramObj = new Object();
			paramObj.orderNoArr = orderNoArr;
			um_ajax_post(
					INDEX_WEBAPP + "/loanorder/export.do",
					paramObj,
					function (data){
						if (data)
						{
							var name = data.fileName;
							$("#fileName").val(name);
							$("#print_form").submit();
						}
					},
					null);
		}
		else{
			$("body").umDialog("alert" ,{"content":"请先选中要导出的数据",left:"40%"});
		}
	});
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	um_ajax_post(
			INDEX_WEBAPP + "/loanorder/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.loanorder);
			},
			null);
	function _render(data)
	{
		//alert(data[0].author);
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr data-id="loanOrderDetail" >');
//				buffer.push('<td style="display:none">'+beautyNull(data[i].id)+'</td>');
				buffer.push('<td><input type="checkbox" id="checkbox" value="' + beautyNull(data[i].orderNo) + '" /></td>');
				buffer.push('<td>'+beautyNull(data[i].orderNo)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].account)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].quantity)+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].paymode)+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].paystatus)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].createdate)+'</td>');
				buffer.push('<td>'+statusChange(data[i].status)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].expresscode)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].expresscompanyid)+'</td>');
				buffer.push('<td>'+(data[i].remark> 6 ?data[i].remark.substr(0,6) + "..." : data[i].remark )+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].ordertype)+'</td>');
				// buffer.push('<td>'+(data[i].comment> 6 ?data[i].comment.substr(0,6) + "..." : data[i].comment )+'</td>');
				buffer.push('<td>'+beautyNull(data[i].uid)+'</td>');
				buffer.push('<td>'+(data[i].expressmessage> 6 ?data[i].expressmessage.substr(0,6) + "..." : data[i].expressmessage)+'</td>');
				buffer.push('<td style="text-align: center" data-id="'+beautyNull(data[i].orderNo)+'"><a href="#" data-id=detail> 详情 </td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
}

function statusChange(str)
{
	if (str == null || str == undefined)
	{
		return "";
	}
	else if (str == "100")
	{
		// 借书单审核通过
		return "审核通过";
	}
	else if (str == "101")
	{
		// 借书单快递员更新了快递信息
		return "快递更新";
	}
	else if (str == "998")
	{
		// 借书单确认初始状态（待审核订单）：998
		return "发起借书";
	}
	else if (str == "999")
	{
		// 借书单确认初始状态（待审核订单）：998
		return "完成借书";
	}
	else
	{
		// 状态码所代表含义未知
		return "未知状态";
	}
	return str;
}

function getCheckdOrderNo()
{
	var orderNoArr = new Array();
	$("input:checkbox:checked").each(function(){ 
		orderNoArr.push($(this).val());
	}) 
	return orderNoArr;
}
$("#checkall").click(function(){  
	$("input#checkbox").prop("checked",this.checked);  
});  
